<?php
/**
 * SocialEngine
 *
 * @category   Application_Extensions
 * @package    Music
 * @copyright  Copyright 2006-2010 Webligo Developments
 * @license    http://www.socialengine.net/license/
 * @version    $Id: install.php 7244 2010-09-01 01:49:53Z john $
 * @author     Steve
 */

/**
 * @category   Application_Extensions
 * @package    Music
 * @copyright  Copyright 2006-2010 Webligo Developments
 * @license    http://www.socialengine.net/license/
 */
class Music_Installer extends Engine_Package_Installer_Module {
	function onInstall() {
		//
		// install content areas
		//
		$db = $this->getDb ();
		$select = new Zend_Db_Select ( $db );
		
		//INSERT INTO `engine4_core_content` (`content_id`, `page_id`, `type`, `name`, `parent_content_id`, `order`, `params`) VALUES
		

		// profile page
		$select->from ( 'engine4_core_pages' )->where ( 'name = ?', 'user_profile_index' )->limit ( 1 );
		$page_id = $select->query ()->fetchObject ()->page_id;
		
		// music.profile-musics
		

		// Check if it's already been placed
		$select = new Zend_Db_Select ( $db );
		$select->from ( 'engine4_core_content' )->where ( 'page_id = ?', $page_id )->where ( 'type = ?', 'widget' )->where ( 'name = ?', 'music.profile-music' );
		$info = $select->query ()->fetch ();
		
		if (empty ( $info )) {
			
			// container_id (will always be there)
			$select = new Zend_Db_Select ( $db );
			$select->from ( 'engine4_core_content' )->where ( 'page_id = ?', $page_id )->where ( 'type = ?', 'container' )->limit ( 1 );
			$container_id = $select->query ()->fetchObject ()->content_id;
			
			// middle_id (will always be there)
			$select = new Zend_Db_Select ( $db );
			$select->from ( 'engine4_core_content' )->where ( 'parent_content_id = ?', $container_id )->where ( 'type = ?', 'container' )->where ( 'name = ?', 'middle' )->limit ( 1 );
			$middle_id = $select->query ()->fetchObject ()->content_id;
			
			// tab_id (tab container) may not always be there
			$select->reset ( 'where' )->where ( 'type = ?', 'widget' )->where ( 'name = ?', 'core.container-tabs' )->where ( 'page_id = ?', $page_id )->limit ( 1 );
			$tab_id = $select->query ()->fetchObject ();
			if ($tab_id && @$tab_id->content_id) {
				$tab_id = $tab_id->content_id;
			} else {
				$tab_id = null;
			}
			
			// music tab on profile
			$db->insert ( 'engine4_core_content', array ('page_id' => $page_id, 'type' => 'widget', 'name' => 'music.profile-music', 'parent_content_id' => ($tab_id ? $tab_id : $middle_id), 'order' => 10, 'params' => '{"title":"Music","titleCount":true}' ) );
		}
		
		// Check if it's already been placed
		$select = new Zend_Db_Select ( $db );
		$select->from ( 'engine4_core_content' )->where ( 'page_id = ?', $page_id )->where ( 'type = ?', 'widget' )->where ( 'name = ?', 'music.profile-player' );
		$info = $select->query ()->fetch ();
		
		if (empty ( $info )) {
			
			// container_id (will always be there)
			$select = new Zend_Db_Select ( $db );
			$select->from ( 'engine4_core_content' )->where ( 'page_id = ?', $page_id )->where ( 'type = ?', 'container' )->limit ( 1 );
			$container_id = $select->query ()->fetchObject ()->content_id;
			
			// middle_id (will always be there)
			$select = new Zend_Db_Select ( $db );
			$select->from ( 'engine4_core_content' )->where ( 'parent_content_id = ?', $container_id )->where ( 'type = ?', 'container' )->where ( 'name = ?', 'middle' )->limit ( 1 );
			$middle_id = $select->query ()->fetchObject ()->content_id;
			
			// left_id (may not always be there)
			$select = new Zend_Db_Select ( $db );
			$select->from ( 'engine4_core_content' )->where ( 'parent_content_id = ?', $container_id )->where ( 'type = ?', 'container' )->where ( 'name = ?', 'left' )->limit ( 1 );
			$left_id = $select->query ()->fetchObject ();
			if ($left_id && $left_id->content_id)
				$left_id = $left_id->content_id;
			else
				$left_id = null;
			
		// tab_id (tab container) may not always be there
			$select->reset ( 'where' )->where ( 'type = ?', 'widget' )->where ( 'name = ?', 'core.container-tabs' )->where ( 'page_id = ?', $page_id )->limit ( 1 );
			$tab_id = $select->query ()->fetchObject ();
			if ($tab_id && @$tab_id->content_id) {
				$tab_id = $tab_id->content_id;
			} else {
				$tab_id = null;
			}
			
			// music player for profile page
			$db->insert ( 'engine4_core_content', array ('page_id' => $page_id, 'type' => 'widget', 'name' => 'music.profile-player', 'parent_content_id' => ($left_id ? $left_id : $middle_id), 'order' => 5, 'params' => '' ) );
		}
		
		parent::onInstall ();
	}
}
?>
